//
//  CircleLayout.m
//  xinhua_sx
//
//  Created by love on 14-8-17.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#define ITEM_SIZE 130
#import "CircleLayout.h"

@implementation CircleLayout
@synthesize cellCount,center,radius;
- (void)prepareLayout{
    [super prepareLayout];
    CGSize size = self.collectionView.frame.size;
    cellCount=[self.collectionView numberOfItemsInSection:0];
    center=CGPointMake(size.width/2, size.height/2);
    radius = MIN(size.width, size.height) / 2.5;
}
-(CGSize)collectionViewContentSize{
    return [self collectionView].frame.size;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
    attributes.center = CGPointMake(center.x + radius * cosf(2 * path.item * M_PI /cellCount),
                                    
                                    center.y + radius * sinf(2 * path.item * M_PI /cellCount));
    return attributes;
}
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{
    NSMutableArray* attributes = [NSMutableArray array];
    for (NSInteger i=0 ; i < self.cellCount; i++) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    return attributes;
}
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedItemAtIndexPath:(NSIndexPath *)itemIndexPath{
    UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    attributes.alpha = 0.0;
    attributes.center = CGPointMake(center.x, center.y);
    return attributes;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    attributes.alpha = 0.0;
    attributes.center = CGPointMake(center.x, center.y);
    attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
    return attributes;
}
@end
